package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.BrandService;
import com.changgou.pojo.Brand;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ：Raoness
 * @date ：Created in 2019/7/23 15:33
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", brandList);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result findById(@PathVariable Integer id){
        Brand brand = brandService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", brand);
    }

    /**
     * 添加
     * @param brand
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改
     * @param brand
     * @return
     */
    @PutMapping("{id}")
    public Result update(@RequestBody Brand brand,@PathVariable Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    @GetMapping("search")
    public Result findList(@RequestParam Map<String,Object> searchMap){
        List<Brand> brandList = brandService.findList(searchMap);
        return new Result(true, StatusCode.OK, "查询成功", brandList);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("page/{page}/{size}")
    public Result findPage(@PathVariable int page,@PathVariable int size){
        Page<Brand> brands = brandService.findPage(page, size);
        PageResult<Brand> pageResult = new PageResult<>(brands.getTotal(),brands.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    /**
     * 分页查询带参数
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("search/{page}/{size}")
    public Result findPage(@RequestParam Map<String, Object> searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Brand> brands = brandService.findPage(searchMap, page, size);
        PageResult<Brand> pageResult = new PageResult<>(brands.getTotal(), brands.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }
}
